package apis

import (
	"errors"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"github.com/go-admin-team/go-admin-core/sdk/api"
	"tuowei.com/app/other/service/dto"
	"tuowei.com/common/service"
	"tuowei.com/pkg/xlog"
)

var arr []string

type Sms struct {
	api.Api
}

func init() {
	arr = []string{"student", "teacher", "invite"}
}

func (e Sms) Send(c *gin.Context) {
	req := dto.PhoneCodeReq{}
	s := service.AliSms{}
	err := e.MakeContext(c).Bind(&req, binding.JSON).MakeService(&s.Service).Errors
	if err != nil {
		xlog.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	if !in(req.SendType, arr) {
		e.Error(500, errors.New("验证码类型不存在"), "验证码类型不存在")
		return
	}

	err = s.CreateClient(&req, req.SendType)
	if err != nil {
		xlog.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	e.OK(nil, "发送成功")
	return
}

func in(target string, array []string) bool {

	for _, element := range array {
		if target == element {
			return true
		}
	}

	return false

}
